<?php

class Model_ProductReports extends Model {

    function __construct() {

        $this->Lookup = getModel('Model_Lookup');
        $this->Member = getModel('Model_Member');
    }

    /* 獲得報告列表資料
     * $productId int 產品ID
     * $mid int 會員id
     * $reportsId int 報告ID
     *  $is_blog 0是全部 1是blog 2是非blog.
     * $is_publish 0是全部 1是發佈 2是草稿.
     * $member_type 0是全部 1是試用 5是體驗
     * $orderby		string	 	字段名 desc 或 字段名 asc
     * $page		int			當前頁數
     * $size		int			獲取個數
     * return		array		資料數組
     */

    function h_get_reports_list($h_param = array()) {

        $prama = array();
        $prama_key_0 = array('productId', 'mid', 'reportsId', 'is_accept', 'trailist_report', 'expert_report', 'member_type_byclick', 'member_type_byaddtime', 'is_mobile');
        if ($prama_key_0) {
            foreach ($prama_key_0 as $value) {
                $prama[$value] = (!empty($h_param[$value])) ? $h_param[$value] : 0;
            }
        }
        $prama_key_1 = array('page', 'is_publish');
        if ($prama_key_1) {
            foreach ($prama_key_1 as $value) {
                $prama[$value] = (!empty($h_param[$value]) || $h_param[$value] == "0") ? $h_param[$value] : 1;
            }
        }

        $prama['orderby'] = (!empty($h_param['orderby'])) ? $h_param['orderby'] : "TB1.id desc";
        $prama['size'] = (!empty($h_param['size'])) ? $h_param['size'] : 3;
        $prama['keyword'] = (!empty($h_param['keyword'])) ? $h_param['keyword'] : "";
        $prama['is_blog'] = (!empty($h_param['is_blog'])) ? $h_param['is_blog'] : 2;
        return $this->getReportsList($prama['productId'], $prama['mid'], $prama['reportsId'], $prama['orderby'], $prama['page'], $prama['size'], $prama['is_accept'], $prama['keyword'], $prama['trailist_report'], $prama['expert_report'], $prama['is_blog'], $prama['member_type_byclick'], $prama['is_publish'], $prama['member_type_byaddtime'], $prama['is_mobile']);
    }

    /* 獲得報告列表資料
     * $productId int 產品ID
     * $mid int 會員id
     * $reportsId int 報告ID
     *  $is_blog 0是全部 1是blog 2是非blog.
     * $is_publish 0是全部 1是發佈 2是草稿.
     * $member_type 0是全部 1是試用 5是體驗
     * $orderby		string	 	字段名 desc 或 字段名 asc
     * $page		int			當前頁數
     * $size		int			獲取個數
     * return		array		資料數組
     */

    function getReportsList($productId = 0, $mid = 0, $reportsId = 0, $orderby = "TB1.id desc", $page = 1, $size = 3, $is_accept = 0, $keyword = '', $trailist_report = 0, $expert_report = 0, $is_blog = 2, $member_type_byclick = 0, $is_publish = 1, $member_type_byaddtime = 0, $is_mobile = 0) {

        $where = $is_mobile ? " 1 " : "TB1.status=1";
        if (intval($is_accept))
            $where .=" and TB1.is_accept=" . intval($is_accept);
        if (intval($productId))
            $where .=" and TB1.oid=" . intval($productId);
        if (intval($mid))
            $where .=" and TB1.mid=" . intval($mid);
        if (intval($reportsId))
            $where .=" and TB1.id=" . intval($reportsId);
        if ($keyword)
            $where .=" and (TB1.title like '%" . $keyword . "%')";
        if (intval($is_blog) == 1) {
            $where .=" and TB1.is_blog=1";
        } elseif (intval($is_blog) == 2) {
            $where .=" and TB1.is_blog=0";
        }
        if (intval($is_publish) == 1) {
            $where .=" and TB1.is_publish=1";
        } elseif (intval($is_publish) == 2) {
            $where .=" and TB1.is_publish=0";
        }


        if ($trailist_report) {
            $paramc = array();
            $paramc['field'] = "report";
            $paramc['where'] = "status=1";
            $paramc['order'] = "id desc"; //"id asc"
            $paramc['group'] = "";
            $paramc['limit'] = 2;
            $paramc['pagesize'] = "";
            $paramc['page'] = "";
            $paramc['join'] = "";
            $paramc['on'] = "";
            $dataindex = db_query_hash('hilite_index_trailist_report', $paramc);

            $indexids = "";
            if ($dataindex['result']) {
                foreach ($dataindex['result'] as $value) {
                    $indexids.=$indexids ? "," . $value['report'] : $value['report'];
                }
                if ($indexids)
                    $where.="  and TB1.id in (" . $indexids . ")";
            }
        }

        if ($expert_report) {
            $paramd = array();
            $paramd['field'] = "report";
            $paramd['where'] = "status=1";
            $paramd['order'] = "id desc"; //"id asc"
            $paramd['group'] = "";
            $paramd['limit'] = 3;
            $paramd['pagesize'] = "";
            $paramd['page'] = "";
            $paramd['join'] = "";
            $paramd['on'] = "";
            $dataexpert_report = db_query_hash('hilite_index_expert_report', $paramd);

            $indexids = "";
            if ($dataexpert_report['result']) {
                foreach ($dataexpert_report['result'] as $value) {
                    $indexids.=$indexids ? "," . $value['report'] : $value['report'];
                }
                if ($indexids)
                    $where.="  and TB1.id in (" . $indexids . ")";
            }
        }

        $db = 'product_reports';
        $group = "";
        if (intval($member_type_byclick)) {
            $where.="  and TB2.member_type=" . intval($member_type_byclick);
            $group = "TB1.mid";
            $db = "(select *,(click+click_base_num) as clicks from product_reports order by clicks desc)";
        }

        if (intval($member_type_byaddtime)) {
            $where.="  and TB2.member_type=" . intval($member_type_byaddtime);
            $group = "TB1.mid";
            $db = "(select * from product_reports order by addtime desc)";
        }


        $param = array();
        $param['field'] = "TB1.id,TB1.oid,TB1.mid,TB1.title,TB1.agegroup,TB1.content_json,TB1.body,(TB1.likes+TB1.likes_num_base) AS likes,TB1.well,TB1.category_score1,TB1.category_score2,TB1.category_score3,TB1.category_score4,TB1.category_score5,TB1.category_score6,TB1.category_score7,TB1.category_score8,TB1.is_valid,TB1.discuz_pid,TB1.is_accept,TB1.grade,TB1.reject_reason,TB1.sequence,TB1.priority,TB1.status,TB1.addtime,TB1.comments,TB1.body_skin,TB1.fb_share_num_base,TB1.is_publish,TB2.photo,TB2.birthday,TB2.username,TB1.is_blog,TB2.member_type,(TB1.click+TB1.click_base_num) as click,TB1.lastmodify";
        $param['where'] = $where;
        $param['order'] = $orderby; //"id asc"
        $param['group'] = $group;
        $param['limit'] = "";
        $param['pagesize'] = $size;
        $param['page'] = $page;
        $param['join'] = "LEFT JOIN member_imore";
        $param['on'] = "TB1.mid = TB2.id";
        $data = db_query_hash($db, $param);

        if ($data['result']) {
            if (intval($reportsId) && $data['result'][0]) {

                $where = "status=1 and is_accept=1 and  id<" . $data['result'][0]['id'];
                if (intval($productId))
                    $where .=" and oid=" . intval($productId);
                if (intval($mid))
                    $where .=" and mid=" . intval($mid);

                $param = array();
                $param['field'] = "id,title";
                $param['where'] = $where;
                $param['order'] = 'id desc'; //"id asc"
                $param['group'] = "";
                $param['limit'] = "1";
                $param['pagesize'] = '';
                $param['page'] = '';
                $param['join'] = "";
                $param['on'] = "";
                $dataNext = db_query_hash('product_reports', $param);

                if ($dataNext['result'])
                    $data['next'] = $dataNext['result'][0];

                $where = "status=1  and is_accept=1 and id>" . $data['result'][0]['id'];
                if (intval($productId))
                    $where .=" and oid=" . intval($productId);
                if (intval($mid))
                    $where .=" and mid=" . intval($mid);

                $param = array();
                $param['field'] = "id,title";
                $param['where'] = $where;
                $param['order'] = 'id asc'; //"id asc"
                $param['group'] = "";
                $param['limit'] = "1";
                $param['pagesize'] = '';
                $param['page'] = '';
                $param['join'] = "";
                $param['on'] = "";
                $dataPrevious = db_query_hash('product_reports', $param);
                if ($dataPrevious['result'])
                    $data['previous'] = $dataPrevious['result'][0];
            }
        }



        $body_skin = $this->Lookup->getLookup('face_skin');
        $body_skinarr = array();
        if ($body_skin['result']) {
            foreach ($body_skin['result'] as $value) {
                $body_skinarr[$value['id']] = $value['cname'];
            }
        }

        $result = array();
        if ($data['result']) {
            foreach ($data['result'] as $key => $value) {
                $conResult = $this->Member->getMemberCondition($value['mid']);

                if ($conResult['result']) {
                    $value['body_skin'] = $body_skinarr[$conResult['result'][0]['face_skin']];
                    $value['body_skin_id'] = $conResult['result'][0]['face_skin'];
                }

                $pat = '/<img(.*?)src="(.*?)"(.*?)>/i';
                $replacement = '<a href="${2}" title="" class="img_lightbox"><img${1}src="${2}"${3}></a>';
                $value['body'] = htmlspecialchars(preg_replace($pat, $replacement, htmlspecialchars_decode($value['body'])));
                $result[$key] = $value;
            }
            $data['result'] = $result;
        }



        return $data;
    }

}

?>